#include "stdafx.h"
#include <cassert>
#include <MyControls/PlayerImpl.h>
#include <MyControls/Sequence.h>
#include <MyControls/GoToFrameDlg.h>

namespace My {

void PlayerImpl::OnTimeFirstFrame()
{
	GetSequence()->SetCurrentFrame(0);
}

void PlayerImpl::OnUpdateTimeFirstFrame(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty() 
		&& 0 != GetSequence()->GetCurrentFrame());
}

void PlayerImpl::OnTimeLastFrame()
{
	GetSequence()->SetCurrentFrame(GetSequence()->GetDuration()-1);
}

void PlayerImpl::OnUpdateTimeLastFrame(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty() 
		&& GetSequence()->GetDuration()-1 != GetSequence()->GetCurrentFrame());
}

void PlayerImpl::OnTimeLoop()
{
	ToggleLoop();
}

void PlayerImpl::OnUpdateTimeLoop(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty());
	pCmdUI->SetCheck(GetLoop() ? 1 : 0);
}

void PlayerImpl::OnTimeNextFrame()
{
	GetSequence()->SetCurrentFrame(GetSequence()->GetCurrentFrame()+1);
}

void PlayerImpl::OnUpdateTimeNextFrame(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty() 
		&& GetPlayMode() == PlayStop 
		&& GetSequence()->GetDuration() - 1 != GetSequence()->GetCurrentFrame());
}

void PlayerImpl::OnTimePause()
{
	SetPlayMode(PlayStop);
}

void PlayerImpl::OnUpdateTimePause(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty());
	pCmdUI->SetRadio(GetPlayMode() == PlayStop);
}

void PlayerImpl::OnTimePlayForward()
{
	SetPlayMode(PlayForward);
}

void PlayerImpl::OnUpdateTimePlayForward(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty());
	pCmdUI->SetRadio(GetPlayMode() == PlayForward);
}

void PlayerImpl::OnTimePlayBackward()
{
	SetPlayMode(PlayBackward);
}

void PlayerImpl::OnUpdateTimePlayBackward(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty());
	pCmdUI->SetRadio(GetPlayMode() == PlayBackward);
}

void PlayerImpl::OnTimeWorkAreaStart()
{
	GetSequence()->SetCurrentFrame(GetSequence()->GetWorkArea().first);
}

void PlayerImpl::OnUpdateTimeWorkAreaStart(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty() 
		&& GetSequence()->GetWorkArea().first != GetSequence()->GetCurrentFrame());
}

void PlayerImpl::OnTimeWorkAreaEnd()
{
	GetSequence()->SetCurrentFrame(GetSequence()->GetWorkArea().last-1);
}

void PlayerImpl::OnUpdateTimeWorkAreaEnd(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty() 
		&& GetSequence()->GetWorkArea().last-1 != GetSequence()->GetCurrentFrame());
}

void PlayerImpl::OnTimePreviousFrame()
{
	GetSequence()->SetCurrentFrame(GetSequence()->GetCurrentFrame()-1);
}

void PlayerImpl::OnUpdateTimePreviousFrame(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty() 
		&& GetPlayMode() == PlayStop 
		&& 0 != GetSequence()->GetCurrentFrame());
}

afx_msg void PlayerImpl::OnTimeGotoFrame()
{
	Sequence & c = *GetSequence();
	My::GoToFrameDlg dlg(AfxGetMainWnd());
	dlg.m_FrameNum = c.GetPreviewFrame() + c.GetStart();
	if (IDOK != dlg.DoModal())
		return;
	c.SetCurrentFrame(dlg.m_FrameNum - c.GetStart());
}

afx_msg void PlayerImpl::OnUpdateTimeGotoFrame(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(!GetSequence()->IsEmpty());
}

void PlayerImpl::OnTimePlayPauseForward()
{
	if (GetPlayMode() == PlayStop)
		SetPlayMode(PlayForward);
	else
		SetPlayMode(PlayStop);
}

void PlayerImpl::OnUpdateTimePlayPauseForward(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(TRUE);
}

void PlayerImpl::OnTimePlayPauseBackward()
{
	if (GetPlayMode() == PlayStop)
		SetPlayMode(PlayBackward);
	else
		SetPlayMode(PlayStop);
}

void PlayerImpl::OnUpdateTimePlayPauseBackward(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(TRUE);
}

void PlayerImpl::OnTimeFollow() 
{
	SetPlayMode(PlayFollow);
}

void PlayerImpl::OnUpdateTimeFollow(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!GetSequence()->IsEmpty() && GetSequence()->CanStartFollow());
	pCmdUI->SetRadio(GetPlayMode() == PlayFollow);
}
				  
void PlayerImpl::OnTimeAutoFollow() 
{
	ToggleAutoFollow();
}

void PlayerImpl::OnUpdateTimeAutoFollow(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!GetSequence()->IsEmpty());
	pCmdUI->SetCheck(GetAutoFollow() ? 1 : 0);
}

} //namespace My
